package com.sjsm.sjsmscenerecord;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjsm.sjsmscenerecord.constant.Constant;
import com.sjsm.sjsmscenerecord.utils.BitmaptoCard;
import com.sjsm.sjsmscenerecord.utils.ImageHelper;
import com.sjsm.sjsmscenerecord.utils.ShowMsgUtils;
import com.sjsm.sjsmscenerecord.view.MyImageView;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by hasee on 2017/8/23.
 */

public class PhotoSettingActivity extends AppCompatActivity {
    @Bind(R.id.rl_photo)
    RelativeLayout rl_photo;
    @Bind(R.id.tv_cancle)
    TextView tvCancle;
    @Bind(R.id.setting_oval_btn)
    ImageView settingOvalBtn;
    @Bind(R.id.setting_rect_btn)
    ImageView settingRectBtn;
    @Bind(R.id.setting_revocation_btn)
    ImageView settingRevocationBtn;
    @Bind(R.id.tv_determine)
    TextView tvDetermine;

    private String setPath;
    private MyImageView iv_setting;
    //在画圈圈还是画框框
    private MyImageView.ActionType currentType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_setting);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        currentType = MyImageView.ActionType.Circle;
        settingOvalBtn.setImageResource(R.drawable.image_edit_oval_select);
        setPath = getIntent().getStringExtra("photo_path");
        if (setPath != null) {
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            Bitmap bitmap = BitmapFactory.decodeFile(setPath);
            if (bitmap != null) {
                iv_setting = new MyImageView(this, bitmap);
                iv_setting.setLayoutParams(layoutParams);
                rl_photo.addView(iv_setting);
            } else {
                ShowMsgUtils.ShowToast(this, "没有找到相应的图片...");
            }

        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (iv_setting != null) {
            return iv_setting.onTouchEvent(event);
        }
        return super.onTouchEvent(event);
    }

    @OnClick({R.id.tv_cancle, R.id.setting_oval_btn, R.id.setting_rect_btn, R.id.setting_revocation_btn, R.id.tv_determine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancle:        //取消
                setResult(Constant.PHOTO_SETTING);
                finish();
                break;
            case R.id.setting_oval_btn: //画圈圈
                if (currentType == MyImageView.ActionType.Rect) {
                    currentType = MyImageView.ActionType.Circle;
                    settingOvalBtn.setImageResource(R.drawable.image_edit_oval_select);
                    settingRectBtn.setImageResource(R.drawable.image_edit_rect);
                    if (iv_setting != null) {
                        iv_setting.setPhotoType(MyImageView.ActionType.Circle);
                    }
                }

                break;
            case R.id.setting_rect_btn: //画框框
                if (currentType == MyImageView.ActionType.Circle) {
                    currentType = MyImageView.ActionType.Rect;
                    settingOvalBtn.setImageResource(R.drawable.image_edit_oval);
                    settingRectBtn.setImageResource(R.drawable.image_edit_rect_select);
                    if (iv_setting != null) {
                        iv_setting.setPhotoType(MyImageView.ActionType.Rect);
                    }
                }
                break;
            case R.id.setting_revocation_btn:   //撤回
                if (iv_setting != null) {
                    iv_setting.back();
                }
                break;
            case R.id.tv_determine: //确定
                if (iv_setting!=null){
                    int end = setPath.lastIndexOf(File.separator);
                    String dir = setPath.substring(0, end+1);
                    //这里进行修改,重新设置了一个路径
                    String file_name= ImageHelper.createPhotoFileName();
                    boolean b = BitmaptoCard.saveBmpToSd(dir+file_name, iv_setting.getBitmap(), 100);
                    if (b){
                        Intent intent = new Intent();
                        intent.putExtra("isSetting",true);
                        intent.putExtra("path",dir+file_name);
                        setResult(Constant.PHOTO_SETTING,intent);

                        finish();
                    }else{
                        ShowMsgUtils.ShowToast(this,"替换失败");
                    }
                }
                break;
        }
    }
}
